pepperstad.StructureService = function (opts) {

    this.structures = [];
    this.paper = opts.paper;
    this.highlighedStructure = null;


    var config = pepperstad.config;

    this.addStructure = function (struct) {
        this.structures.push(struct);
    };

    this.loadStructures = function (map, loader) {
        this.structures = this.deserializeStructures(map, loader);
        log("Structure data loaded OK! " + this.structures.length + " loaded.");
        this.drawAllStructures();
        this.reportStructuralCapacity();
    };

    this.highlight = function (struct) {
        if (this.highlighedStructure) {
            this.highlighedStructure.highlight(false);
        }
        if (struct) {
            struct.highlight(true);
        }
        this.highlighedStructure = struct;
    };

    this.exportStructures = function () {
        download(config.structuresFilename,
            JSON.stringify(this.serializableStructures(), null, 2));
    };

    this.drawAllStructures = function () {
        log("Drawing all structures");
        var structs = this.structures,
            struct, i;
        for (i = 0; i < structs.length; i++) {
            struct = structs[i];
            struct.drawStructure(this.paper);
        }
    };

    this.deserializeStructures = function (map, loader) {
        log("Deserializing structures");
        var data = loader.get("story.structures");
        var arr = [],
            struct, i;
        for (i = 0; i < data.length; i++) {
            struct = data[i];
            arr.push(new pepperstad.Structure({
                poly: struct.poly,
                type: struct.type,
                name: struct.name,
                state: struct.state,
                map: map,
                resourceLoader: loader
            }));
        }
        return arr;
    };

    this.serializableStructures = function () {
        log("Serializing structures");
        var arr = [],
            struct, i;
        var data = this.structures;
        for (i = 0; i < data.length; i++) {
            struct = data[i];
            arr.push(new pepperstad.Structure({
                poly: struct.poly,
                type: struct.type,
                name: struct.name,
                state: struct.state
            }));
        }
        return arr;
    };

    this.deleteStructure = function (struct) {
        this.structures = _.reject(this.structures,
            function (el) {
                return el === struct;
            });
        struct.removeGraphics();
        console.log("Structure deleted: " + struct);
    };

    this.reportStructuralCapacity = function () {
        var tCap = 0,
            rCap = 0;
        _.each(this.structures, function (s) {
            if (_.contains(["residental", "unoccupied"], s.type))
                rCap += s.getCapacity();
            tCap += s.getCapacity();
        });
        log("Total residental/unoccupied capacity: " + rCap);
        log("Total building capacity: " + tCap);
    };
};
